/** 
 * 
 * @author geloin 
 * @date 2012-5-5 涓婂崍9:31:52 
 */
package com.wcs.spring.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wcs.spring.entity.User;
import com.wcs.spring.service.UserService;
import com.wcs.spring.util.Const;
import com.wcs.spring.util.PageUtil;

@Controller
@RequestMapping(value = "mgt/user")
public class UserController {
	@Resource(name = "userService")
	private UserService userService;

	@RequestMapping(value = "userList")
	public ModelAndView userList(User user, PageUtil pageUtil, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User sessionUser = (User) request.getSession().getAttribute(Const.SESSION_SYS_USER);
		user.setSchoolId(sessionUser.getSchoolId());
		Map<String, Object> map = new HashMap<String, Object>();
		PageInfo<User> pager = null;
		try {
			PageHelper.startPage(pageUtil.getPageNum(), pageUtil.getPageSize());
			List<User> list = this.userService.queryUserList(user, pageUtil);
			pager = new PageInfo(list);
		}
		catch (Exception e) {
			System.out.println("error:" + e.getMessage());
		}
		map.put("pager", pager);
		map.put("pageUtil", pageUtil);
		map.put("user", user);
		return new ModelAndView("mgt/user/user_list", map);
	}

	// @RequestMapping(value = "rectorList")
	// public ModelAndView rectorList(User user, PageUtil pageUtil, HttpServletRequest request,
	// HttpServletResponse response) throws Exception {
	// Map<String, Object> map = new HashMap<String, Object>();
	// user.setUserType("xz");
	// List<User> rectorList = this.userService.queryUserByType(user);
	// map.put("rectorList", rectorList);
	// return new ModelAndView("mgt/rector/rector_list", map);
	// }
	//
	// @RequestMapping(value = "teacherList")
	// public ModelAndView teacherList(User user, PageUtil pageUtil, HttpServletRequest request,
	// HttpServletResponse response) throws Exception {
	// Map<String, Object> map = new HashMap<String, Object>();
	// user.setUserType("js");
	// List<User> teacherList = this.userService.queryUserByType(user);
	// map.put("teacherList", teacherList);
	// return new ModelAndView("mgt/teacher/teacher_list", map);
	// }
	//
	// @RequestMapping(value = "eduAdminList")
	// public ModelAndView eduAdminList(User user, PageUtil pageUtil, HttpServletRequest request,
	// HttpServletResponse response) throws Exception {
	// Map<String, Object> map = new HashMap<String, Object>();
	// user.setUserType("jw");
	// List<User> eduAdminList = this.userService.queryUserByType(user);
	// map.put("eduAdminList", eduAdminList);
	// return new ModelAndView("mgt/eduAdmin/eduAdmin_list", map);
	// }

	@RequestMapping(value = "toUserSave")
	public ModelAndView toUserSave(User user, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userType", user.getUserType());
		map.put("user", null);
		return new ModelAndView("mgt/user/user_add", map);
	}

	@RequestMapping(value = "toUserUpdate")
	public ModelAndView toUserUpdate(Integer id, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = userService.queryUserById(id);
		map.put("user", user);
		map.put("userType", user.getUserType());
		return new ModelAndView("mgt/user/user_add", map);
	}

	@RequestMapping(value = "toUserShow")
	public @ResponseBody
	User toUserShow(Integer id, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = userService.queryUserById(id);
		// map.put("user", user);
		// return new ModelAndView("mgt/user/user_show", map);
		return user;
	}

	@RequestMapping(value = "userSave")
	public ModelAndView userSave(User user, HttpServletRequest request) throws Exception {
		User sessionUser = (User) request.getSession().getAttribute(Const.SESSION_SYS_USER);
		if (!"zj".equals(sessionUser.getUserType())) {
			user.setSchoolId(sessionUser.getSchoolId());
		}
		Date date = new Date();
		user.setCreateTime(date);
		user.setUpdateTime(date);
		user.setCreateUser(sessionUser.getUserName());
		user.setUpdateUser(sessionUser.getUserName());
		user.setSchoolId(sessionUser.getSchoolId());
		userService.addUser(user);
		return new ModelAndView("redirect:/mgt/user/userList.do?userType=" + user.getUserType());
	}

	@RequestMapping(value = "userUpdate")
	public ModelAndView userUpdate(User user, HttpServletRequest request) throws Exception {
		User sessionUser = (User) request.getSession().getAttribute(Const.SESSION_SYS_USER);
		user.setUpdateTime(new Date());
		user.setUpdateUser(sessionUser.getUserName());
		userService.updateUser(user);
		return new ModelAndView("redirect:/mgt/user/userList.do?userType=" + user.getUserType());
	}

	@RequestMapping(value = "userDelete")
	public ModelAndView userDelete(User user, HttpServletResponse response) throws Exception {
		userService.delUser(user);
		return new ModelAndView("redirect:/mgt/user/userList.do?userType=" + user.getUserType());
	}

	@RequestMapping(value = "checkUserName")
	public @ResponseBody
	boolean checkUserName(User user, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// User sessionUser = (User) request.getSession().getAttribute(Const.SESSION_SYS_USER);
		// user.setSchoolId(sessionUser.getSchoolId());
		boolean flag = false;
		User user2 = userService.queryUserByParam(user);
		if (user2 == null) {
			flag = true;
		}
		return flag;
	}
}
